package com.youlai.boot.modules.category.controller;

import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.security.access.prepost.PreAuthorize;

import com.youlai.boot.common.result.Result;
import com.youlai.boot.common.annotation.RepeatSubmit;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.modules.category.model.Category;
import com.youlai.boot.modules.category.model.CategoryForm;
import com.youlai.boot.modules.category.model.CategoryPageQuery;
import com.youlai.boot.modules.category.model.CategoryVO;
import com.youlai.boot.modules.category.model.TreeItemVO;
import com.youlai.boot.modules.category.service.CategoryService;

import jakarta.validation.Valid;
import com.baomidou.mybatisplus.core.metadata.IPage;

import lombok.extern.slf4j.Slf4j;

/**
 * 分类前端控制层
 *
 * @author youlaitech
 * @since 2024-08-27 10:31
 */
@Tag(name = "14. 分类接口", description = "分类管理接口")
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api/v1/category")
public class CategoryController {
    private final CategoryService categoryService;

    @Operation(summary = "分类列表")
    @GetMapping("/page")
    public PageResult<CategoryVO> listCategories(CategoryPageQuery query) {
        IPage<CategoryVO> page = categoryService.getCategoryPage(query);
        return PageResult.success(page);
    }

    @Operation(summary = "分类树")
    @GetMapping("/tree")
    public Result<List<TreeItemVO>> getCategoryTree() {
        List<TreeItemVO> categoryTree = categoryService.getCategoryTree();
        return Result.success(categoryTree);
    }

    @Operation(summary = "分类详情")
    @GetMapping("/{categoryId}")
    public Result<CategoryVO> getCategory(@PathVariable Long categoryId) {
        CategoryVO category = categoryService.getCategoryById(categoryId);
        return Result.success(category);
    }

    @Operation(summary = "添加分类")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:dept:add')")
    @RepeatSubmit
    public Result<Long> addCategory(@Valid @RequestBody CategoryForm category) {
        Long id = categoryService.saveCategory(category);
        return Result.success(id);
    }

    @Operation(summary = "获取分类表单数据")
    @GetMapping("/{categoryId}/form")
    public Result<Category> getCategoryForm(@PathVariable Long categoryId) {
        Category category = categoryService.getById(categoryId);
        return Result.success(category);
    }

    @Operation(summary = "修改分类")
    @PutMapping("/{categoryId}")
    public Result<Boolean> updateCategory(@PathVariable Long categoryId,
            @RequestBody CategoryForm category) {
        category.setId(categoryId);
        categoryService.updateCategory(category);
        return Result.success(true);
    }

    @Operation(summary = "批量删除分类")
    @DeleteMapping("/{categoryIds}")
    public Result<Boolean> deleteCategories(@PathVariable String categoryIds) {
        if (StringUtils.isEmpty(categoryIds)) {
            return Result.failed("分类ID不能为空");
        }

        try {
            List<Long> ids = Arrays.stream(categoryIds.split(","))
                    .map(Long::parseLong)
                    .collect(Collectors.toList());
            boolean result = categoryService.removeByIds(ids);
            return Result.judge(result);
        } catch (NumberFormatException e) {
            return Result.failed("分类ID格式不正确");
        }
    }
}
